﻿using System;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

namespace JQMergeFramework
{
    public class RankItem : MonoBehaviour
    {
        public UISwitchChild bgSwitchChild,iconSwitchChild;

        public Image headImg;

        public Text nameText;

        public Text scoreText;

        public Text rankText;
        
        private RankData mRankData;

        
        
        public void Init(RankData rData,int index)
        {
            this.mRankData = rData;

            RefreshUi();
        }

        public void PlayMoveAnim(float time,Action<int> call)
        {
            DOTween.Kill(this.transform.GetInstanceID());

            Sequence sq = DOTween.Sequence().SetId(this.transform.GetInstanceID());
            this.gameObject.SetActive(false);
            sq.AppendInterval(time);
            sq.AppendCallback(() =>
            {
                this.gameObject.SetActive(true);
                this.transform.position = new Vector3(1500, this.transform.position.y, 0);
            });
            sq.Append(this.transform.DOMoveX(544, 0.4f));
            sq.OnComplete(() =>
            {
                call.Invoke(this.mRankData.rank);
            });
            sq.Play();
        }


        void RefreshUi()
        {
            
            bgSwitchChild.index = this.mRankData.types;
            iconSwitchChild.index = this.mRankData.types <3?this.mRankData.types:3;

            nameText.text = mRankData.name;
            scoreText.text = mRankData.score.ToString();
            rankText.text = mRankData.rank.ToString();
        }

    }
}